package com.example.opencv_demo.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.opencv_demo.R;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<ActivityBean> mData;
    private Context context;

    public MyAdapter(List<ActivityBean> mData, Context context) {
        this.mData = mData;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_text, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
          if (holder instanceof MyViewHolder){
              ((MyViewHolder) holder).textView.setText(mData.get(position).content);
              final int p=position;
              ((MyViewHolder) holder).textView.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      context.startActivity(new Intent(context,mData.get(p).aClass));
                  }
              });
          }
    }

    @Override
    public int getItemCount() {
        return mData==null?0:mData.size();
    }

    private static class MyViewHolder extends RecyclerView.ViewHolder{
        public TextView textView;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            textView=itemView.findViewById(R.id.tv_content);
        }
    }

    public static class ActivityBean{
        public String content;
        public Class<?> aClass;

        public ActivityBean(String content, Class<?> aClass) {
            this.content = content;
            this.aClass = aClass;
        }
    }
}
